﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace danci
{
    public partial class Form1 : Form
    {
        private string script = "";
        FRMModel fmModel = new FRMModel();

        public Form1()
        {
            InitializeComponent();
            this.script = System.IO.File.ReadAllText(Path.Combine(AppContext.BaseDirectory,"jsfunc.js"));
            //设置绑定
            labWordlist.DataBindings.Add(nameof(labWordlist.Text),fmModel,nameof(fmModel.CurrentFnUI));
            labWord.DataBindings.Add("Text", fmModel, nameof(fmModel.CurrentWordDisp));
            labTotal.DataBindings.Add("Text", fmModel, nameof(fmModel.TotalWords));
            //labNumber.DataBindings.Add("Text", fmModel, nameof(fmModel.WordIndex));
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            this.fmModel.goNext();
            await searchWord(fmModel.CurrentWord);
        }

        private async void button2_Click(object sender, EventArgs e)
        {
            
            fmModel.goPrev();
            await searchWord(fmModel.CurrentWord);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var frm = new frmWords();
            frm.parentFm = fmModel;
            frm.Show();
        }

        private void wv_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
        {
            wv.ExecuteScriptAsync(this.script);
        }


        private async void Form1_Load(object sender, EventArgs e)
        {
            await wv.EnsureCoreWebView2Async();
            wv.Source = new Uri("http://www.eudic.cn/dicts/en/hello");
            fmModel.loadList();
            Form1_Resize(null, null);
        }

        private async Task searchWord(string word) {
            //string script = $"$('#dic-input').val({word})";
            //await wv.ExecuteScriptAsync(script);
            await wv.EnsureCoreWebView2Async();
            wv.Source =new Uri( $"http://www.eudic.cn/dicts/en/{word}");
        }

       
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.A || e.KeyCode== Keys.PageUp)
            {
                button1_Click(null, null);
                e.Handled = true;
            }
            if (e.KeyCode == Keys.D || e.KeyCode == Keys.PageDown)
            {
                button2_Click(null, null);
                e.Handled = true;
            }
        }

        private void wv_Enter(object sender, EventArgs e)
        {
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.ActiveControl == wv) {
                button2.Focus();
            }
        }

        private void wv_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void button2_Leave(object sender, EventArgs e)
        {
            
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            const int offsettop = 180;
            wv.Width = this.Width - panel1.Width;
            wv.Top = -offsettop;
            wv.Height = Height + offsettop;
        }
    }
}
